﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using System.IO;
using PloobsEngine.Commands;
using PloobsEngine.Input;

namespace PloobsEngine.DataAquisition
{
    /// <summary>
    /// TODO
    /// </summary>
    public class GenericNumberFileSaver : DataHandlers
    {
            public GenericNumberFileSaver(String fileName, Keys saveKey)
        {
            this.fileName = fileName;
            this.saveKey = saveKey;    

            InputPlayableKeyBoard ip1 = new SimpleConcreteKeyboardInputPlayable(StateKey.PRESS, saveKey, saveFile, EntityType.TOOLS);
            BindKeyCommand mm = new BindKeyCommand(ip1, BindAction.ADD);
            CommandProcessor.getCommandProcessor().SendCommandSyncronous(mm, InputAdvanced.MyName);          
        }


        private void raw()
        {
            foreach (var itemx in values.Keys)
            {
                StreamWriter sw = new StreamWriter(fileName + "_" + itemx + ".csv" , true);
                int i = 0;
                sw.WriteLine(DateTime.Now + " " + itemx + " RAW");
                foreach (var item in values[itemx])
                {
                    for (int j = 0; j < item.Numbers.Length; j++)                    
                    {
                        if (j == 0)
                        {
                            sw.Write(i + "," + item.Numbers[j]);                            
                            if (j != item.Numbers.Length - 1)
                            {
                                sw.Write(",");
                            }
                            else
                            {
                                sw.Write("\n");
                            }

                        }
                        else if (j == item.Numbers.Length - 1)
                        {
                            sw.WriteLine(item.Numbers[j]);
                        }
                        else
                        {
                            sw.Write(item.Numbers[j] + ",");
                        }
                      
                    }
                    i++;
                    
                }
                sw.WriteLine("END");
                sw.Close();          
            }
            
        }
     

        private void saveFile(InputPlayableKeyBoard ipk)
        {    
             raw();
            
        }
        
        #region DataHandlers Members
        private String fileName = null;       
        Keys? saveKey = null;        
        private Dictionary<String, List<GenericNumberDataFormat>> values = new Dictionary<String, List<GenericNumberDataFormat>>();
       
                
        public void HandleData(DataFormat data)
        {
            if (data is GenericNumberDataFormat)
            {
                GenericNumberDataFormat dat = data as GenericNumberDataFormat;
                if (!values.ContainsKey(dat.Origin))
                {
                    values[dat.Origin] = new List<GenericNumberDataFormat>();
                    values[dat.Origin].Add(dat);

                }
                else
                {
                    values[dat.Origin].Add(dat);
                }
            }
        }

        public Type[] GetHandledType
        {
            get { return new Type[] { typeof(GenericNumberDataFormat) }; }
        }

        #endregion
    }
}
